<?php
class user extends My_Controller
{
	function user()
	{
		parent::__construct();
	}
	
	function post()
	{
		$data['title']	= 'Đăng tin rao vặt';
		if ($this->input->post('cmdpost'))
		{
			$maxfilesize    = 1;
			$size_image		= $maxfilesize * 1024 * 1024;
			$dir_path		= getcwd();
			$allowedExts	= array('JPG', 'JPEG', 'PNG', 'GIFT');
				
			$ext			= explode(".", $_FILES["txtimage"]["name"]);
			$extension 		= strtoupper(end($ext));
			if ((($_FILES["txtimage"]["type"] == "image/gift") || ($_FILES["txtimage"]["type"] == "image/jpeg") || ($_FILES["txtimage"]["type"] == "image/png") || ($_FILES["txtimage"]["type"] == "image/pjpeg")) && ($_FILES["txtimage"]["size"] < $size_image) && in_array($extension, $allowedExts))
			{
				$_SESSION['ERROR_UPLOAD_FILE']	= "";
				$filename	= $_FILES["txtimage"]["name"];
				if(!is_dir('upload'))
					mkdir('upload', 0777, true);
				
				move_uploaded_file($_FILES["txtimage"]["tmp_name"], $dir_path."/upload/".$_FILES["txtimage"]["name"]);
			}
			else
			{
				$_SESSION['ERROR_UPLOAD_FILE']	= 'Không đúng định dạng file';
				$filename	= "";
			}
			
			$params		= array(
								'user_id'			=> (!empty($_SESSION[base_url().'user_id']) && isset($_SESSION[base_url().'user_id'])) ? ($_SESSION[base_url().'user_id']) : 0,
								'title'				=> mysql_real_escape_string($this->input->post('txttitle')),
								'phone'				=> $this->input->post('txtphone'),
								'image'				=> ($filename != "") ? $filename : NULL,
								'cat_1_id'			=> $this->input->post('select_cat1'),
								'cat_2_id'			=> $this->input->post('select_subcat'),
								'cat_3_id'			=> $this->input->post('select_subsubcat'),
								'price_id'			=> $this->input->post('select_price'),
								'province_id'		=> $this->input->post('select_city'),
								'distinct_id'		=> $this->input->post('select_distinct'),
								'content'			=> $this->input->post('txtcontent'),
								'createdate'		=> time(),
								'updatedate'		=> time(),
								'display_startdate'	=> time(),
								'display_enddate'	=> time() + 30*24*60*60,
								'status'			=> 1,
								'click'				=> 0,
								'hot'				=> 0
			);
			$this->mod_lib->insert('products', $params);
			redirect(base_url().'dang-tin-rao-vat');
		}
		$this->load->view('sites/users/post', $data);
	}
	
	function getsubcat()
	{
		$cat_id		= $_POST['cat_id'];
		if (!empty($cat_id))
		{
			$sub_cat	= $this->mod_lib->load_all('categories', '', array('cat_id'=>$cat_id, 'level'=>2), '', '', array());
			echo '<option value="">==== Chọn nhu cầu ==========</option>';
			foreach ($sub_cat as $row_subcat)
			{
				echo '<option value='.$row_subcat['id'].'>'.$row_subcat['name'].'</option>';	
			}
		}
		else
			echo '<option value="">==== Chọn nhu cầu ==========</option>';
	}
	
	function getsubsubcat()
	{
		$subcat_id	= $_POST['subcat_id'];
		if (!empty($subcat_id))
		{
			$subsub_cat	= $this->mod_lib->load_all('categories', '', array('cat_id'=>$subcat_id, 'level'=>3), '', '', array());
			echo '<option value="">==== Chọn chủng loại ======</option>';
			foreach ($subsub_cat as $row_subsubcat)
			{
				echo '<option value='.$row_subsubcat['id'].'>'.$row_subsubcat['name'].'</option>';	
			}
		}
		else
			echo '<option value="">==== Chọn chủng loại ======</option>';
	}
	
	function getdistinct()
	{
		$city_id	= $_POST['city_id'];
		if (!empty($city_id))
		{
			$distinct	= $this->mod_lib->load_all('distincts', '', array('province_id'=>$city_id), '', '', array());
			echo '<option value="">==== Chọn Quận/huyện ======</option>';
			foreach ($distinct as $row_dis)
			{
				echo '<option value='.$row_dis['id'].'>'.$row_dis['name'].'</option>';	
			}
		}
		else
			echo '<option value="">==== Chọn Quận/huyện ======</option>';
	}
	
	function logout()
	{
		$_SESSION[base_url().'user_login']			= NULL;
		$_SESSION[base_url().'user_show']			= NULL;
		$_SESSION[base_url().'user_id']				= NULL;
		$_SESSION[base_url().'user_login']			= NULL;
		$_SESSION[base_url().'user_email']			= NULL;
		$_SESSION[base_url().'user_contact']		= NULL;
		$_SESSION[base_url().'error_user_login']	= NULL;
		redirect(base_url());
	}
}